/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package miselania.almacenamiento;

/**
 *
 * @author INFORMATICA
 */
import java.util.*;
import java.util.List;

public class Relacion  implements
        java.io.Serializable , Iterable<Atributo>  {
    
    
    //lista de atributos
    private List<Atributo> atributos;

    public Relacion() {
       atributos = new ArrayList<Atributo>();
    }
    
    //constructor de la relacion

    public Relacion(List<Atributo> atributos) {
        this.atributos = new ArrayList<Atributo>(atributos);
    }

    protected void adicionarAtributo(Atributo atributo){
        atributos.add(atributo);
        
    }
    public int getNumeroDeAtributos(){
        return atributos.size();
    }
    

    public void eliminar(String nom){
        
         int i =0;
         Atributo n = new Atributo(null,null);
                  
        for (Atributo atributo : atributos) {
           if(atributo.getNombre().equals(nom)){
              n= atributo;
           }
        }
        atributos.remove(n);
    }
    @Override
    public Iterator<Atributo> iterator() {
        return atributos.iterator();
    }
    public Atributo getAtributo(int i){
        return atributos.get(i);
    }
    
    public List<Atributo> getAtributos() {
        return atributos;
    }
    
    public int getIndiceAtributo(String atr){
        int i =0;
        for (Atributo atributo : atributos) {
           if(atributo.getNombre().equals(atr)){
               return i;
           }
        }
        return -1;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("(");
        for (int i = 0; i < getNumeroDeAtributos()-1; i++) {
            sb.append(getAtributo(i)).append(", ");
        }
        sb.append(getAtributo(getNumeroDeAtributos()-1 )).append(")");
        
        return sb.toString();
    }
    
}
